﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZH_Controls.ViewModel;
using HZH_Controls.Common;

namespace HZH_Controls.Controls.KeyBord
{
    public partial class PhraseEasySelect : UserControl
    {
        /// <summary>
        /// 词组数据源
        /// </summary>
        public List<PhraseViewModel> DataSorce { get; set; }

        public string PhraseField { get; set; }

        public string PhraseVal { get; set; }

        public string PhraseCode { get; set; }

        public string PhraseType { get; set; }

        public string BindFieldAlias { get; set; }

        public string BindField { get; set; }


        public TextBox ParentTxt { get; set; }

        public List<FieldViewModel> AliasList = new List<FieldViewModel>();

        public Action<string,string,string> CallBack { get; set; }


        private List<PhraseViewModel> Data = new List<PhraseViewModel>();

        public PhraseEasySelect()
        {
            InitializeComponent();
         
        }

        private void PhraseEasySelect_Load(object sender, EventArgs e)
        {
            Data = DataSorce.Where(p=> p.Category == PhraseType).ToList();
            dtView.DataSource = Data;
            new  FieldAliasHelper().SetAlias(AliasList,dtView,BindFieldAlias);
            if(ParentTxt  != null)
            {
                ParentTxt.TextChanged += txtInput_TextChanged;
                ParentTxt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter && this.Visible)
            {
                if (dtView.SelectedRows.Count > 0)
                {
                    this.Parent.Hide();
                    if (CallBack != null)
                    {
                        string name = string.Empty;
                        if (dtView.Columns.Contains(PhraseField))
                        {
                            name = Convert.ToString(dtView.SelectedRows[0].Cells[PhraseField].Value);
                        }

                        string val = string.Empty;
                        if (dtView.Columns.Contains(PhraseVal))
                        {
                            val = Convert.ToString(dtView.SelectedRows[0].Cells[PhraseVal].Value);
                        }

                        if (ParentTxt != null)
                        {
                            ParentTxt.Text = name;
                        }
                        CallBack(val, name, BindField);
                    }
                }
            }
            
        }

        void txtInput_TextChanged(object sender, EventArgs e)
        {
            string val = ParentTxt.Text;
            if(val.Length > 0)
            {
                var list = Data.Where(p => p.Name.Contains(val)  || p.PinYin.ToUpper().Contains(val.ToUpper())).ToList();
                dtView.DataSource = list;
            }
            else
            {
                dtView.DataSource = Data;
            }
           
        }




        private void dtView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex >= 0 )
            {
                this.Parent.Hide();
                if (CallBack != null)
                {
                    string name = string.Empty;
                    if(dtView.Columns.Contains(PhraseField))
                    {
                        name = Convert.ToString(dtView.Rows[e.RowIndex].Cells[PhraseField].Value);
                    }

                    string val = string.Empty;
                    if (dtView.Columns.Contains(PhraseVal))
                    {
                        val = Convert.ToString(dtView.Rows[e.RowIndex].Cells[PhraseVal].Value);
                    }
                    
                    if(ParentTxt != null)
                    {
                        ParentTxt.Text = name;
                    }
                    CallBack(val, name, BindField);
                }
            }
          
        }

        /// <summary>
        /// 收起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label39_Click(object sender, EventArgs e)
        {
            this.Parent.Hide();
        }
    }
}
